﻿using Newtonsoft.Json;

namespace BlazorAppDemo.Common
{
    public static class JsonHelper
    {
        static AsyncLocal<JsonSerializerSettings> jsonSerializerSettingsAsync = new AsyncLocal<JsonSerializerSettings>();

        public static JsonSerializerSettings JsonSerializerSettings
        {
            get
            {
                if (jsonSerializerSettingsAsync.Value == null)
                {
                    lock (jsonSerializerSettingsAsync)
                    {
                        var settings = new JsonSerializerSettings();
                        settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
                        settings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                        settings.NullValueHandling = NullValueHandling.Ignore;
                        settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                        settings.Formatting = Formatting.Indented;
                        jsonSerializerSettingsAsync.Value = settings;
                    }
                }
                return jsonSerializerSettingsAsync.Value;
            }
        }

        /// <summary>
        /// 对象序列化为字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj, JsonSerializerSettings);
        }

        /// <summary>
        /// 对象序列化为字符串,并以UTF8转成数组返回
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] ToJsonBytes(this object obj)
        {
            var json = ToJson(obj);
            return System.Text.Encoding.UTF8.GetBytes(json);
        }

        /// <summary>
        /// 字符串反序列化为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T ToObject<T>(this string json)
        {
            return JsonConvert.DeserializeObject<T>(json, JsonSerializerSettings);
        }

        /// <summary>
        /// 字符串反序列化为对象
        /// </summary>
        /// <param name="json"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object ToObject(this string json, Type type)
        {
            return JsonConvert.DeserializeObject(json, type, JsonSerializerSettings);
        }

        /// <summary>
        /// Json字符串格式化
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FormatJson(this string str)
        {
            //格式化json字符串
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(str);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ' '
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            }
            else
            {
                return str;
            }
        }
    }
}
